package com.kapiclientsdk.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

//生成二维码工具类
public class QRCodeGenerator {

    /**
     * 宽高
     */
    private final int width;
    private final int height;

    /**
     * 文件类型，也可以说是后缀
     */
    private final String format;

    /**
     *配置信息
     */
    private final Map<EncodeHintType, Object> hints;

    public QRCodeGenerator(int width, int height, String format) {
        this.width = width;
        this.height = height;
        this.format = format;
        this.hints = new HashMap<>();
        //字符集
        this.hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        //错误纠正级别
        //错误纠正功能允许二维码在部分损坏或污损的情况下仍能被正确识别。
        this.hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

        //二维码白色边框
        this.hints.put(EncodeHintType.MARGIN, 4);
    }

    /**
     * 生成二维码方法
     *
     * @param content 二维码存储信息
     * @return
     */
    public  String generateQRCodeBase64(String content) {

        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            //BitMatrix 二维码的位矩阵
            BitMatrix bitMatrix = new MultiFormatWriter()
                    .encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            //将位矩阵转换为BufferedImage对象，表示二维码图像的像素数据
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ImageIO.write(image, format, os);
            return "data:image/" + format + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray());
        } catch (WriterException | IOException e) {
            throw new RuntimeException("Failed to generate QR code", e);
        }
    }
    /**
     * 生成二维码方法(带地址)
     *
     * @param content 二维码存储信息
     * @return
     */
    public void  generateQRCodeBase64(String content,String path,String fileName) throws FileNotFoundException {

        File directory = new File(path);
        if (!directory.exists()){
            directory.mkdirs();
        }
        File file = new File(path,fileName);

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        try {
            //BitMatrix 二维码的位矩阵
            BitMatrix bitMatrix = new MultiFormatWriter()
                    .encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            //将位矩阵转换为BufferedImage对象，表示二维码图像的像素数据
            BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ImageIO.write(image, format, fileOutputStream);
        } catch (WriterException | IOException e) {
            throw new RuntimeException("Failed to generate QR code", e);
        }
    }

}
